﻿using System;
using System.Collections.Generic;
using MediaPortal.GUI.Library;
using dlCentral.Settings.Data;

namespace dlCentral.GUI {
    public class GUIListItemComparer : IComparer<GUIListItem> {
        private ItemSortMethod _sortMethod;
        private bool _sortAsc;

        public GUIListItemComparer(ItemSortMethod sortMethod, bool ascending) {
            _sortMethod = sortMethod;
            _sortAsc = ascending;
        }

        public int Compare(GUIListItem item1, GUIListItem item2) {
            if (item1 == item2) return 0;
            if (item1 == null) return -1;
            if (item2 == null) return -1;
            if (item1.IsFolder && item1.Label == "..") return -1;
            if (item2.IsFolder && item2.Label == "..") return -1;
            if (item1.IsFolder && !item2.IsFolder) return -1;
            else if (!item1.IsFolder && item2.IsFolder) return 1;

            SiteSortDetails subtitlesSortDetails1;
            SiteSortDetails subtitlesSortDetails2;
            try {
                subtitlesSortDetails1 = (SiteSortDetails)item1.AlbumInfoTag;
                subtitlesSortDetails2 = (SiteSortDetails)item2.AlbumInfoTag;
            }
            catch {
                return 0;
            }

            switch (_sortMethod) {

                case ItemSortMethod.ItemDate:
                    if (_sortAsc)
                    {
                        if (subtitlesSortDetails1.Date == subtitlesSortDetails2.Date)
                        {
                            if (subtitlesSortDetails1.ListPosition > subtitlesSortDetails2.ListPosition) return 1;
                            if (subtitlesSortDetails1.ListPosition < subtitlesSortDetails2.ListPosition) return -1;
                        }
                        else
                        {
                            return DateTime.Compare(subtitlesSortDetails1.Date, subtitlesSortDetails2.Date);
                        }
                    }
                    else
                    {
                        if (subtitlesSortDetails1.Date == subtitlesSortDetails2.Date)
                        {
                            if (subtitlesSortDetails1.ListPosition > subtitlesSortDetails2.ListPosition) return -1;
                            if (subtitlesSortDetails1.ListPosition < subtitlesSortDetails2.ListPosition) return 1;
                        }
                        else
                        {
                            return DateTime.Compare(subtitlesSortDetails2.Date, subtitlesSortDetails1.Date);
                        }
                    }
                    return 0;

                case ItemSortMethod.ItemName:
                    if (_sortAsc) {
                        if (subtitlesSortDetails1.Name == subtitlesSortDetails2.Name) {
                            if (subtitlesSortDetails1.ListPosition > subtitlesSortDetails2.ListPosition) return 1;
                            if (subtitlesSortDetails1.ListPosition < subtitlesSortDetails2.ListPosition) return -1;
                        }
                        else {
                            return string.Compare(subtitlesSortDetails1.Name, subtitlesSortDetails2.Name, true);
                        }
                    }
                    else {
                        if (subtitlesSortDetails1.Name == subtitlesSortDetails2.Name) {
                            if (subtitlesSortDetails1.ListPosition > subtitlesSortDetails2.ListPosition) return -1;
                            if (subtitlesSortDetails1.ListPosition < subtitlesSortDetails2.ListPosition) return 1;
                        }
                        else {
                            return string.Compare(subtitlesSortDetails2.Name, subtitlesSortDetails1.Name, true);
                        }
                    }
                    return 0;
            }
            return 0;
        }
    }
}

